use crate::global_context::GlobalContext;
use cocoa::appkit::{NSColor, NSWindow};
use cocoa::base::{id, nil};
use serde::Deserialize;
use std::sync::Mutex;
use tauri::State;

#[derive(Deserialize)]
pub struct ColorRgba {
    r: f64,
    g: f64,
    b: f64,
    a: f64,
}

#[tauri::command]
pub fn set_bar_color(
    state: State<'_, Mutex<GlobalContext>>,
    color_rgba: ColorRgba,
    label: String,
) -> u32 {
    let state = state.lock().unwrap();
    for win in &state.win_array {
        if win.label() != label {
            continue;
        }
        let ns_window = win.ns_window().unwrap() as id;
        unsafe {
            let bg_color = NSColor::colorWithRed_green_blue_alpha_(
                nil,
                color_rgba.r / 255.0,
                color_rgba.g / 255.0,
                color_rgba.b / 255.0,
                color_rgba.a,
            );
            ns_window.setBackgroundColor_(bg_color);
        }
    }
    0
}
